estimation_data <- 
  estimation_data %>%
  mutate(kim_member = case_when(D %in% c("GERINDRA", "GOLKAR", "DEMOKRAT", "PAN") ~ 1,
                                TRUE ~ 0))


mod7 <- feols(avg_score_democ_support ~ pre_post*validated_election_outcome, data = estimation_data %>% filter(kim_member == 1), cluster = "ID")
mod7a <- feols(V_32_A ~ pre_post*validated_election_outcome, data = estimation_data %>% filter(kim_member == 1), cluster = "ID")
mod7b <- feols(V_32_B ~ pre_post*validated_election_outcome, data = estimation_data %>% filter(kim_member == 1), cluster = "ID")
mod7c <- feols(V_32_C ~ pre_post*validated_election_outcome, data = estimation_data %>% filter(kim_member == 1), cluster = "ID")

mod8 <- feols(avg_score_democ_support ~ pre_post*validated_election_outcome, data = estimation_data %>% filter(kim_member == 0), cluster = "ID")
mod8a <- feols(V_32_A ~ pre_post*validated_election_outcome, data = estimation_data %>% filter(kim_member == 0), cluster = "ID")
mod8b <- feols(V_32_B ~ pre_post*validated_election_outcome, data = estimation_data %>% filter(kim_member == 0), cluster = "ID")
mod8c <- feols(V_32_C ~ pre_post*validated_election_outcome, data = estimation_data %>% filter(kim_member == 0), cluster = "ID")


mods <- list(mod7a, mod7b, mod7c, mod7, mod8a, mod8b, mod8c, mod8)

table_df <-
  modelsummary(mods,
               stars = c("*" = 0.1, "**" = 0.05, "***" = 0.01),
               coef_map = c(
                 "pre_post" = "Post Election",
                 "validated_election_outcomeNot Elected" = "Outcome: Lost",
                 "pre_post:validated_election_outcomeNot Elected" = "Post x Lost"
               ),
               gof_map = c("nobs", "r.squared"),
               output = "data.frame"
  ) %>%
  mutate(term = ifelse(statistic == "std.error", "", term)) %>%
  select(-c(part, statistic)) %>%
  mutate_all(as.character)



# Append the new row to the table
latex_table <-
  table_df %>%
  kableExtra::kable("latex",
                    booktabs = T,
                    longtable = T,
                    escape = F,
                    cap = "\\label{tab:kim_table}Main and Individual Item Analysis, by Coalition Group",
                    padding = 0,
                    col.names = linebreak(c("", "(1)", "(2)", "(3)", "(4)", "(5)", "(6)", "(7)" ,"(8)"), c("l", rep("c", 8))),
                    align = c("l", rep("c", 8)),
                    linesep = "") %>%
  kableExtra::row_spec(6, hline_after = T) %>%
  kableExtra::add_header_above(
    header = c(
      " " = 1,
      "(A)" = 1,
      "(B)" = 1,
      "(C)" = 1,
      "Index" = 1,
      "(A)" = 1,
      "(B)" = 1,
      "(C)" = 1,
      "Index" = 1
    ),
    align = c("l", rep("c", 8))
  ) %>%
  kableExtra::kable_styling(
    font_size = 9,
    latex_options = c("scale_down",
                      "hold_position",
                      "striped")) %>%
  kableExtra::add_header_above(
    header = c(
      " " = 1,
      "KIM" = 4,
      "non-KIM" = 4
    ),
    align = c("l", rep("c", 4))
  )

cat("\\renewcommand{\\arraystretch}{0.9}\n", file = "./outputs/tables/table_a8.tex")
cat(latex_table, file = "./outputs/tables/table_a8.tex", append = TRUE)


